package org.gap.jseed.injection;

import javassist.ClassPool;
import javassist.CtClass;
import javassist.NotFoundException;

/**
 * Attempts to locate the CtClass.
 * @author gpelcha
 *
 */
public class CtClassLocator {
	private static ClassPool pool;
	static {
		pool = ClassPool.getDefault();
	}
	
	public static CtClass getCtClass(Class<?> javaClass) {
		try {
			return pool.get(javaClass.getName());
		} catch (NotFoundException e) {
			return addClassPathAndReturn(javaClass);			
		}
	}

	private static CtClass addClassPathAndReturn(Class<?> javaClass) {
		try {
			pool.appendClassPath(javaClass.getPackage().getName());
			return pool.get(javaClass.getName());
		} catch (NotFoundException e) {
			throw new IllegalArgumentException(e.getMessage());
		}
	}
}
